//
//  LJHTabbarPlayBtn.m
//  HEARME
//
//  Created by mac on 16/2/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHTabbarPlayBtn.h"

@implementation LJHTabbarPlayBtn

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createSubviews];
    }
    return self;
}

-(void)createSubviews
{
    self.backgroundColor = [UIColor clearColor];
    self.frame =CGRectMake(WIDTH*0.4, -WIDTH*0.07, WIDTH*0.2, HEIGHT*0.2);
    self.userInteractionEnabled = YES;
    
    //背景
    self.btnBackground = [[LJHBaseImageView alloc]initWithFrame:self.bounds];
    
    self.btnBackground.layer.masksToBounds = YES;
    self.btnBackground.layer.cornerRadius = WIDTH*0.1;
    [self addSubview:self.btnBackground];
    self.btnBackground.tag = 1000;
    [_btnBackground release];
    
    //条
    UIImageView *tiao = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,WIDTH*0.2, WIDTH*0.2)];
    tiao.backgroundColor = [UIColor clearColor];
    tiao.image = [UIImage imageNamed:@"tabbar_np_loop@2x.png"];
    tiao.center = CGPointMake(WIDTH*0.1, WIDTH*0.1);
    [self.btnBackground addSubview:tiao];
    
    //旋转ImageView
    self.musicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.musicBtn.frame = CGRectMake(0, 0, WIDTH*0.15, WIDTH*0.15);
    self.musicBtn.backgroundColor = [UIColor clearColor];
    [self.musicBtn sd_setImageWithURL:[[[LJHFMData shareFMDB] selectTrackImageData] lastObject] forState:UIControlStateNormal placeholderImage:PLACEHOLDER];
    self.musicBtn.layer.masksToBounds = YES;
    self.musicBtn.layer.cornerRadius = WIDTH*0.075;
    self.musicBtn.center = CGPointMake(WIDTH*0.1, WIDTH*0.1);
    [tiao addSubview:self.musicBtn];
    
    
    //播放按钮
    self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playBtn.backgroundColor = [UIColor clearColor];
    self.playBtn.frame = CGRectMake(0, 0, WIDTH*0.15, WIDTH*0.15);
    self.playBtn.layer.masksToBounds = YES;
    self.playBtn.layer.cornerRadius = WIDTH*0.075;
    self.playBtn.center = CGPointMake(WIDTH*0.1, WIDTH*0.1);
    [self.playBtn setImage:[UIImage imageNamed:@"tabbar_np_play@2x.png"] forState:UIControlStateNormal];
    self.playBtn.alpha = 0.6;
    [tiao addSubview:self.playBtn];
    [self.playBtn addTarget:self action:@selector(playMusic:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btnBackground.userInteractionEnabled = YES;
    tiao.userInteractionEnabled = YES;
    
    
    
    
}
-(void)playMusic:(UIButton *)btn
{
    
    LJHPlayerViewController *player = [LJHPlayerViewController sharaMusicPlayer];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    [player.player resume];
    [self.window.rootViewController presentViewController:naVC animated:YES completion:^{
        
        [self startCircle];
        
    }];
    
}
-(void)startCircle
{
#pragma mark - musicBtn旋转动画
    //改变view Z轴
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    //旋转的起始值 从零开始转
    animation.fromValue = [NSNumber numberWithInt:0];
    
    //最终旋转的角度是360°
    animation.toValue = [NSNumber numberWithInt:M_PI * 2];
    
    //旋转时长
    animation.duration = 5;
    
    //重复次数 0 为无限次
    animation.repeatCount = NSIntegerMax;
    
    //旋转结束后是否要逆向返回原位置
    animation.autoreverses = NO;
    
    //是否按照结束位置继续旋转
    animation.cumulative = YES;
    
    //给view的layer层添加动画,key是标记  随便起名字
    [self.musicBtn.layer addAnimation:animation forKey:@"circle"];
    self.musicBtn.layer.speed = 1.0;
    self.playBtn.alpha = 0;
    self.playBtn.enabled = NO;
}

-(void)stopCircle
{
    self.musicBtn.layer.speed = 0;
    self.playBtn.alpha = 1;
    self.playBtn.enabled = YES;
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
